﻿import QtQuick 2.0

// 图表曲线管理 (请搭配C++食用)
Rectangle{

    id: root

    // public ========================================================

    color: "transparent"

    property var cChartInterface: CChart       // 在此传入C++注册进来的图表，来和前端图表绑定

//    property real lineWidthBase: Math.min(width, height) * 0.001
    property real lineWidthBase: 1

    property bool   showChartTitle: true
    property bool   showTitleBtn: true
    property string title: ""          // 图表标题
    property real   title_fontsize: config.fontsize

    property string xTitle: "时间" + (xTitleMode===0 ? "(min)" : "(s)")   // 适配能谱图
    property string yTitle: "Counts(cps)"

    property real minXRangeSize: Math.min(0.5, 240 / maxX)            // 适配能谱图，固定为最多放大4分钟的数据量
    property real minYRangeSize: 0.2

    // 显示full还是显示缩放后的内容，不知道是适配啥的
    property bool isFullImage: false

    // 适配能谱图
    property real lastXRange: 1     // 临时量，记录上一次的网格密度
    property int  xTitleMode: 0     // 0分钟 1秒

    property var imageChart

    // 坐标轴保留小数点位数
    property int  decimalX: 0
    property int  decimalY: 0

    // 坐标轴分段数(-1表示默认)
    property int  tickCountX: -1
    property int  tickCountY: -1

    property real fontsize: config.fontsize - 2*dp

    property real margin: 10*dp

    // 修改xy轴坐标内容
    property bool isFixedXTickTitle: false
    property bool isFixedYTickTitle: false
    property var xTickTitleList: []
    property var yTickTitleList: []

    // 滚轮控制和图表缩放体系
    property bool showScrollBar: true
    property int mouseWheelYControl: -1     // 鼠标滚轮控制内容 -1不控制 0图表缩放 1图表滚动
    property int mouseWheelXControl: -1

    // 鼠标拖动
    property bool mouseDrag: false

    // 鼠标划过去找线
    property bool mouseHighlightCurve: true
    property bool mouseHighlightPoint: true

    // private =======================================================

    // 从后端传回来的数据
    property real minX: 0
    property real maxX: 0
    property real minY: 0
    property real maxY: 0

    property var curveInfo  // 用来存放后端传回来的曲线
    property var normPointList: []
    property var bezierPointList: []
    property var curveTypeList: []
    property var scatterTypeList: []
    property var lineWidthList: []
    property var lineColorList: []


    // 每次刷新完成发送信号
    signal refreshFinished()


    // 坐标轴分段  [TODO] 用户可固定分段数 --------->
    // 因为有缩放，坐标轴后续改到C++计算了
    property var tickPercentX: [0, 0.2, 0.4, 0.6, 0.8, 1]
    property var tickPercentY: [0, 0.2, 0.4, 0.6, 0.8, 1]
    property var tickValueX: [0, 1, 2, 3, 4, 5]
    property var tickValueY: [0, 1, 2, 3, 4, 5]


    function isBetween(a, b, c){
        if(a<=b && b<=c){
            return true
        }if(a>=b && b>=c){
            return true
        }
        return false
    }

    // 坐标轴尺寸
    property int axisSizeL: axisYWidth     // (给坐标轴标题留的地方
    property int axisSizeB: axisXHeight
    property int legendHeight: 0
    property real axisXHeight: 60
    property real axisYWidth: text_axisY_max.contentWidth + 50

    // 用来算坐标轴留多大空间的
    Text{ id: text_maxX; text: maxX.toFixed(decimalY); font.pixelSize: root.fontsize; visible: false }
    Text{ id: text_maxY; text: maxY.toFixed(decimalX); font.pixelSize: root.fontsize; visible: false }

    property real   leftMargin: margin + (yTitle==="" ? 0 : fontsize+2*dp) + (text_maxY.contentWidth)
    property real   bottomMargin: margin + (xTitle===""? 0 : fontsize+2*dp) + (fontsize) + (showScrollBar ? 20*dp : 0)
    property real   rightMargin: margin + (text_maxX.contentWidth * 0.5) + (showScrollBar ? 16*dp : 0)
    property real   topMargin: margin + ((showChartTitle || showTitleBtn) ? titleLine.height+5*dp : 0*dp)

    Connections{
        target: cChartInterface
        onSigRefresh: {
            refreshData()
        }
        onSigResetRange:{
            scrollBar_H.r1 = 0
            scrollBar_H.r2 = 1
            scrollBar_V.r1 = 0
            scrollBar_V.r2 = 1
        }
    }


    function refreshData(){     // 刷新从后端获取的数据

//        console.time("Chart-GetCurveInfoTime")
        if(isFullImage){
            curveInfo = cChartInterface.getCurveInfo_fullImage()
        }else{
            curveInfo = cChartInterface.getCurveInfo()
        }

//        console.timeEnd("Chart-GetCurveInfoTime")



        minX = curveInfo.chartMinX
        minY = curveInfo.chartMinY
        maxX = curveInfo.chartMaxX
        maxY = curveInfo.chartMaxY

        normPointList = curveInfo.normPointList
        bezierPointList = curveInfo.bezierPointList
        curveTypeList = curveInfo.curveType
        scatterTypeList = curveInfo.scatterType
        lineWidthList = curveInfo.lineWidth
        lineColorList = curveInfo.color

        tickPercentX = curveInfo.tickPercentX
        tickPercentY = curveInfo.tickPercentY
        tickValueX = curveInfo.tickValueX
        tickValueY = curveInfo.tickValueY

        canvas.requestPaint()
    }

    function resetRange(){

        console.log("chart-resetRange")

        cChartInterface.setShowRangeX(1-lastXRange, 1)
        cChartInterface.setShowRangeY(0, 1)
        scrollBar_H.r1 = 0
        scrollBar_H.r2 = 1
        scrollBar_V.r1 = 1-lastXRange
        scrollBar_V.r2 = 1
        cChartInterface.refresh()
    }
    function initRange(){

        console.log("chart-initRange")

        cChartInterface.setShowRangeX(0, 1)
        cChartInterface.setShowRangeY(0, 1)
        scrollBar_H.r1 = 0
        scrollBar_H.r2 = 1
        scrollBar_V.r1 = 0
        scrollBar_V.r2 = 1
        cChartInterface.refresh()
    }

    // layout =========================================================
    // 图表
    Rectangle{ anchors.fill: canvas }       // 图表背景
    CustomChartCanvas{
        id: canvas
    }

    CustomChartMouseArea{
        id: mouseArea
        anchors.fill: canvas
    }



    // 坐标轴标题
    Text{       // 纵坐标Y(左)
        id: text_yTitle
        text: yTitle
        rotation: -90
        anchors.centerIn: canvas
        anchors.horizontalCenterOffset: -canvas.width/2 - fontsize*0.6 - text_maxY.contentWidth - 3*dp
        font.pixelSize: fontsize
        font.bold: true
        color: Qt.rgba(0, 0, 0, 0.7)
    }
    Text{       // 横坐标X(底)
        id: text_xTitle
        text: xTitle
        anchors.centerIn: canvas
        anchors.verticalCenterOffset: canvas.height/2 + fontsize * 1.6 + 3*dp
        color: Qt.rgba(0, 0, 0, 0.7)
        font.pixelSize: fontsize
        font.bold: true
    }
    // 滚动条(下)
    CustomScrollBar{
        id: scrollBar_V
        color: "#CCC"
        width: canvas.width
        visible: showScrollBar
        height: 14*dp
        x: canvas.x
        y: text_xTitle.y + text_xTitle.height + 5*dp
        onRangeChanged: {
            cChartInterface.setShowRangeX(r1, r2)
            cChartInterface.refresh()
        }
    }
    // 滚动条(右)
    CustomScrollBar{
        id: scrollBar_H
        color: "#CCC"
        width: 14*dp
        height: canvas.height
        visible: showScrollBar
        y: canvas.y
        x: canvas.x + canvas.width + 5*dp
        onRangeChanged: {
            cChartInterface.setShowRangeY(1-r2, 1-r1)
            cChartInterface.refresh()
        }
    }


    // 最下面坐标轴的线
    Rectangle{      // 横坐标
        height: 1.6; width: canvas.width
        x: leftMargin; y: parent.height - bottomMargin
        color: Qt.rgba(0, 0, 0, 0.15)
    }
    // 纵坐标没线 只有横线0.0

    // 坐标轴数字和线
    // Y轴线
    Repeater{
        model: tickPercentY.length
        Rectangle{
            x: leftMargin;  y: topMargin + canvas.height * (1-tickPercentY[index])
            width: canvas.width;  height: 1
            color: Qt.rgba(0, 0, 0, 0.1)
            Text{
                x: -contentWidth-3
                y: -1*dp
                anchors.verticalCenter: parent.verticalCenter
//                text: isFixedYTickTitle ? yTickTitleList[index] : ((maxY-minY)*modelData + minY).toFixed(decimalX).toString()
                text: tickValueY[index].toFixed(0)
                font.pixelSize: fontsize-2
                color: Qt.rgba(0, 0, 0, 0.5)
            }
        }
    }
    // x轴线
    Repeater{
        model: tickPercentX.length
        Rectangle{
            x: leftMargin + canvas.width * tickPercentX[index];  y: topMargin
            width: 1;   height: canvas.height
            color: Qt.rgba(0, 0, 0, 0.15)
            Text{
                y: canvas.height + 3*dp
                anchors.horizontalCenter: parent.horizontalCenter
                text: {
                    // 适配能谱图
                    if(xTitleMode === 0){       // 分钟
                        return ((tickValueX[index]) / 60).toFixed(0)
                    }else{                      // 秒
                        return (tickValueX[index]).toFixed(0)
                    }
                }
                visible: {
                    return  tickValueX[index] % 60 === 0   &&       // 只显示整数能谱图网格
                            (tickValueX.length < 20 ? true : index % Math.floor(tickValueX.length/10)===0)      // 防止网格数太多

                }
                font.pixelSize: fontsize-2
                color: Qt.rgba(0, 0, 0, 0.5)
            }
        }
    }

    // 图表标题和菜单栏
    CustomChartTitleLine{
        id: titleLine
        visible: showTitleBtn || showChartTitle
        x: margin; y: margin
        width: parent.width - 2*margin

    }


}
